<?php

namespace App\Http\Controllers;


use App\Http\Requests;
use ToolClass\ImageUpload;
use Validator;
use Illuminate\Support\Facades\Request;
use ServiceClass\Categories;

class CategoriesController extends Controller
{
    //
    public function __construct(Categories $categories){

        $this->categories = $categories;
    }


    public function addCategories(){

        $input  = Request::only('categories_name','categories_introduce');
        $validator = Validator::make($input, [
            'categories_name' => 'required|max:16',
            'categories_introduce' => 'required',
        ]);

        if ($validator->fails()) {
            return $validator->messages();
        }

        $result = $this->categories->addCategories($input);
        if($result == 1){


            return json_encode(['msg'=>"添加分类成功"]);

        }else{


            return json_encode(['msg'=>"添加分类失败"]);
        }
    }


    public function deleteCategories(){

        $categories_id   = Request::only('categories_id');

        $result = $this->categories->deleteCategories($categories_id);

        if($result == 1){

            return json_encode(['msg'=>"删除成功"]);
        }else{


            return json_encode(['msg'=>"服务器错误"]);
        }
    }


    public function searchCategories(){

        $categories_id   = Request::only('categories_id');

        $result = $this->categories->searchCategories($categories_id);

        if($result == 0){

            return json_encode(['msg'=>"服务器错误"]);
        }else{

            return json_encode($result);
        }
    }

    public function updateCategories(){

        $input  = Request::only('categories_name','categories_introduce','categories_id');

        $validator = Validator::make($input, [
            'categories_name' => 'required|max:16',
            'categories_introduce' => 'required',
        ]);

        if ($validator->fails()) {
            return $validator->messages();
        }

        $result = $this->categories->updateCategories($input);
        if($result == 1){

            return json_encode(['msg'=>"修改分类成功"]);
        }else{

            return json_encode(['msg'=>"修改分类失败"]);
        }
    }
    public function selectCategoriesList(){

        $result = $this->categories->selectCategoriesList();
        return json_encode($result);
    }
}
